package com.efast.cafe.portal.service.common.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.common.PortalCommonCollectionBean;
import com.efast.cafe.portal.dao.common.PortalCommonCollectionDao;
import com.efast.cafe.portal.entity.common.PortalCommonCollection;
import com.efast.cafe.portal.service.common.IPortalCommonCollectionService;
import com.efast.cafe.util.StringUtil;
import com.efast.cafe.util.date.DateUtils;
@Service
@Transactional
public class PortalCommonCollectionServiceImpl implements IPortalCommonCollectionService {

	private PortalCommonCollectionDao portalCommonCollectionDao;
	
	@Autowired
	public void setPortalCommonCollectionDao(PortalCommonCollectionDao portalCommonCollectionDao) {
		this.portalCommonCollectionDao = portalCommonCollectionDao;
	}

	

	@Override
	public List<PortalCommonCollectionBean> queryRestDayByYear(Integer year,String username) {
		String companyCode = SpringWebUtils.getRequestCompany();
		
		
		return portalCommonCollectionDao.queryRestDayByYear(year+"",companyCode,username);
	}

	

	@Override
	public void initCompanyRest(Integer year) {
		 String companyCode = SpringWebUtils.getRequestCompany();
		portalCommonCollectionDao.delCompanyRestDay(companyCode,year+"");
		List<PortalCommonCollection> list = new ArrayList<PortalCommonCollection>();
        Calendar calendar=new GregorianCalendar(year,0,1);  
        int i=1;  
       
        String username = SpringWebUtils.getRequestUser();
        while(calendar.get(Calendar.YEAR)<year+1){  
            calendar.set(Calendar.WEEK_OF_YEAR,i++);  
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);  
            if(calendar.get(Calendar.YEAR)==year){  
            	PortalCommonCollection c = new PortalCommonCollection();
            	c.setCompanyCode(companyCode);
            	c.setRestDay(calendar.getTime());
            	c.setCreateUser(username);
            	c.setLastUpdateUser(username);
            	list.add(c);
            }  
            calendar.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);  
            if(calendar.get(Calendar.YEAR)==year){  
            	PortalCommonCollection c = new PortalCommonCollection();
            	c.setCompanyCode(companyCode);
            	c.setRestDay(calendar.getTime());
            	c.setCreateUser(username);
            	c.setLastUpdateUser(username);
            	list.add(c);
            }  
        }
        portalCommonCollectionDao.addRestDayList(list);
		
	}

	/**
	 * 获取一个日期段中的所有日期
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	private  List<String> getDatesBetweenTwoDate(Date beginDate, Date endDate) {    
	    List<String> lDate = new ArrayList<>();
	    lDate.add(DateUtils.format(beginDate));//把开始时间加入集合
	    Calendar cal = Calendar.getInstance();
	    //使用给定的 Date 设置此 Calendar 的时间
	    cal.setTime(beginDate);
	    boolean bContinue = true;
	    while (bContinue) {
	        //根据日历的规则，为给定的日历字段添加或减去指定的时间量
	        cal.add(Calendar.DAY_OF_MONTH, 1);
	        // 测试此日期是否在指定日期之后
	        if (endDate.after(cal.getTime())) {
	            lDate.add(DateUtils.format(cal.getTime()));
	        } else {
	            break;
	        }
	    }
	    if(!lDate.contains(DateUtils.format(endDate))){
	    	lDate.add(DateUtils.format(endDate));//把结束时间加入集合
	    }
	    return lDate;
	}
	


	@Override
	public void addRestDay(Integer year,String startDate, String endDate, String username) {
		String companyCode = SpringWebUtils.getRequestCompany();
		String currentUser = SpringWebUtils.getRequestUser();
		List<String> allDates = getDatesBetweenTwoDate(DateUtils.parse(startDate),DateUtils.parse(endDate));
		List<PortalCommonCollection> addlist = new ArrayList<>();
		
		portalCommonCollectionDao.delUserOrRestDay(year+"",companyCode, username, startDate, endDate);
		
		
		//用户名为空 则添加的是企业日历
//		if(StringUtil.isNullOrBlank(username)){
//			portalCommonCollectionDao.delUserOrRestDay(year+"",companyCode, username, startDate, endDate);
//		}else{//给用户设置工作日历
//			//判断用户是否有工作日历 如果没有 则先复制企业默认日历
//			if(portalCommonCollectionDao.countUserRestDay(year+"", companyCode, username)>0){
//				//先删除startDate和endDate范围内的日历
//				portalCommonCollectionDao.delUserOrRestDay(year+"",companyCode, username, startDate, endDate);
//			}else{
//				//复制企业默认日历给用户日历
//				List<PortalCommonCollectionBean> list = queryRestDayByYear(year,username);
//				for(PortalCommonCollectionBean b : list){
//					if(!allDates.contains(b.getStartDate())){
//						PortalCommonCollection c = new PortalCommonCollection();
//						c.setCompanyCode(companyCode);
//						c.setCreateUser(currentUser);
//						c.setLastUpdateUser(currentUser);
//						c.setRestDay(DateUtils.parse(b.getStartDate()));
//						c.setUsername(username);
//						addlist.add(c);
//					}
//				}
//			}
//		}
		//添加选择日期范围作为工作日历
		for(String d : allDates){
			PortalCommonCollection c = new PortalCommonCollection();
			c.setCompanyCode(companyCode);
			c.setCreateUser(currentUser);
			c.setLastUpdateUser(currentUser);
			c.setRestDay(DateUtils.parse(d));
			c.setUsername(username);
			addlist.add(c);
		}
		if(addlist.size()>0){
			portalCommonCollectionDao.addRestDayList(addlist);	
		}
	}



	@Override
	public void delRestDay(Integer year, String username,String startDate,String endDate) {
		String companyCode = SpringWebUtils.getRequestCompany();
		String currentUser = SpringWebUtils.getRequestUser();
		List<PortalCommonCollection> addlist = new ArrayList<>();
		if(!StringUtil.isEmpty(startDate) && !StringUtil.isEmpty(endDate)){

			portalCommonCollectionDao.delUserOrRestDay(year+"",companyCode, username, startDate, endDate);
			
		}else{//日期都为空 ，初始化用户的日历
			if(!StringUtil.isEmpty(username)){
				portalCommonCollectionDao.delUserOrRestDay(year+"",companyCode, username, "", "");
				List<PortalCommonCollectionBean> list = queryRestDayByYear(year,"");
				for(PortalCommonCollectionBean b : list){
						PortalCommonCollection c = new PortalCommonCollection();
						c.setCompanyCode(companyCode);
						c.setCreateUser(currentUser);
						c.setLastUpdateUser(currentUser);
						c.setRestDay(DateUtils.parse(b.getStartDate()));
						c.setUsername(username);
						addlist.add(c);
				}
				if(addlist.size()>0){
					portalCommonCollectionDao.addRestDayList(addlist);	
				}
			}
		}
	}
	
	
}
